package com.itheima.datastructure.binarysearchtree.binarytree;

public class TreeTraversal {
    public static void main(String[] args) {
        TreeNode root=new TreeNode(new TreeNode(new TreeNode(4),2,null),
                1,new
                TreeNode(new TreeNode(5),3,new TreeNode(6))
        );
        preOrder(root);
        System.out.println();
        inOrder(root);
        System.out.println();
        postOrder(root);
    }

    //前序遍历
    static void preOrder(TreeNode node){
        if(node==null){
            return;
        }
        System.out.print(node.val+"\t");//中值
        preOrder(node.left);//左值
        preOrder(node.right);//右值
    }

    //中序遍历
    static void inOrder(TreeNode node){
        if(node==null){
            return;
        }
        inOrder(node.left);
        System.out.print(node.val+"\t");
        inOrder(node.right);

    }

    //后序遍历
    static void postOrder(TreeNode node){
        if(node==null){
            return ;
        }
        postOrder(node.left);
        postOrder(node.right);
        System.out.print(node.val+"\t");
    }
}
